﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using AutumnEngine.Modules;
using System.Diagnostics;
using AutumnEngine;

namespace AutumnEngine.Modules
{
    public class SoundManagerModule : Module
    {
        Dictionary<string, SoundEffect> _sounds = new Dictionary<string, SoundEffect>();
        Dictionary<string, string> paths = new Dictionary<string, string>();
        Dictionary<string, SoundEffectInstance> soundInstances = new Dictionary<string, SoundEffectInstance>();

        public void AddSound(string name, string path)
        {
            //Remove exception and check properly
            if (Engine.Modules.Contains("Engine.Logging"))
                Engine.Modules.Logging.Write("Adding sound " + name, LogLevel.Debug);
            try
            {
                paths.Add(name, path);
                _sounds.Add(name, Engine.Modules.ContentManager.Load<SoundEffect>(path));
                soundInstances.Add(name, null);
            }
            catch (Exception) { }
        }

        public override void Create()
        {
            this.CanBeDisabled = false;
            this.Removable = false;            
            base.Create();
        }

        public void PlaySound(string name, float volume, float pitch, float pan, bool allowMultiple)
        {
            if (allowMultiple)
            {
                if (soundInstances[name] == null)
                    createIfNull(name);
                soundInstances[name].Volume = volume;
                soundInstances[name].Pitch = pitch;
                soundInstances[name].Pan = pan;
                soundInstances[name].Play();
            }
            else
            {
                //We dont want to play more than one.
                if (soundInstances[name] == null)
                {
                    createIfNull(name);
                    soundInstances[name].Volume = volume;
                    soundInstances[name].Pitch = pitch;
                    soundInstances[name].Pan = pan;
                    soundInstances[name].Play();
                    return;
                }
                if (soundInstances[name].State == SoundState.Playing)
                    return;
                else
                {
                    soundInstances[name].Volume = volume;
                    soundInstances[name].Pitch = pitch;
                    soundInstances[name].Pan = pan;
                    soundInstances[name].Play();
                    soundInstances[name].Play();
                }
            }
        }

        public void PlaySound(string name)
        {
            PlaySound(name, 1f, 1f, 0.5f, false);
        }

        public void PlaySound(string name, bool allowMulti)
        {
            PlaySound(name, 1f, 1f, 0.5f, allowMulti);
        }

        void createIfNull(string name)
        {
            soundInstances[name] = _sounds[name].CreateInstance();
            
        }
    }
}
